package com.robot.orders.service;

import com.robot.orders.dao.SettlementDao;
import com.robot.orders.pojo.Settlement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * settlement服务层
 *
 * @author wuyunbin
 */
@Service
public class SettlementService {
    @Autowired
    private SettlementDao settlementDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<Settlement> findAll() {
        return settlementDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<Settlement> findSearch(Map whereMap, int page, int size) {
        Specification<Settlement> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return settlementDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<Settlement> findSearch(Map whereMap) {
        Specification<Settlement> specification = createSpecification(whereMap);
        return settlementDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public Settlement findById(String id) {
        return settlementDao.findById(id).get();
    }

    /**
     * 增加
     *
     * @param settlement
     */
    public void add(Settlement settlement) {
        //雪花分布式ID生成器
        settlement.setId(idWorker.nextId() + "");
        settlementDao.save(settlement);
    }

    /**
     * 修改
     *
     * @param settlement
     */
    public void update(Settlement settlement) {
        settlementDao.save(settlement);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        settlementDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<Settlement> createSpecification(Map searchMap) {
        return new Specification<Settlement>() {
            @Override
            public Predicate toPredicate(Root<Settlement> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 结算总表id
                String id = "id";
                if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                    predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
                }
                // 代理商id
                String agentId = "agentId";
                if (searchMap.get(agentId) != null && !"".equals(searchMap.get(agentId))) {
                    predicateList.add(cb.like(root.get(agentId).as(String.class), "%" + (String) searchMap.get(agentId) + "%"));
                }
                //剔除软删除数据
                String deletedAt = "deletedAt";
                predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(Date.class)));

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 更改settlement表的status状态0-未提现 1-已提现
     *
     * @param status
     * @param agentId
     * @param settlementDate
     */
    public void updateSettlementStatus(String status, String agentId, String settlementDate) {
        settlementDao.updateSettlementStatus(status, LocalDateTime.now(), agentId, settlementDate);
    }

}
